#!/usr/bin/perl -w

sub multVec {
  my ($num,$vec) = @_;
  if($num =~ "," && $vec !~ ",") { 
    $tmp = $num; 
    $num = $vec; 
    $vec = $tmp;
  }
  my ($x,$y,$z) = vec2list($vec);
  $x*=$num; $y*=$num; $z*=$num;
  return list2vec($x,$y,$z);
};

sub normVec {
  my ($vec) = @_;
  my ($x,$y,$z) = vec2list($vec);
  my $len = sqrt($x**2 + $y**2 + $z**2);
  if($len < 0.00000001) { 
    print STDERR "zero vector!";
    return 0; 
  }
  $x/=$len; $y/=$len; $z/=$len;
  return list2vec($x,$y,$z);
};

sub subtVec {
  my ($v1,$v2) = @_;
  #v1 - v2
  my ($x1,$y1,$z1) = vec2list($v1);
  my ($x2,$y2,$z2) = vec2list($v2);
  $x1 -= $x2;
  $y1 -= $y2;
  $z1 -= $z2;
  $v1 = list2vec($x1,$y1,$z1);
  return $v1;
};

sub addVec {
  my ($v1,$v2) = @_;
  #v1 + v2
  my ($x1,$y1,$z1) = vec2list($v1);
  my ($x2,$y2,$z2) = vec2list($v2);
  $x1 += $x2;
  $y1 += $y2;
  $z1 += $z2;
  $v1 = list2vec($x1,$y1,$z1);
  return $v1;
};

sub lenVec {
  my ($vec) = @_;
  my ($x,$y,$z) = vec2list($vec);
  return sqrt($x**2 + $y**2 + $z**2);
};
 
sub list2vec {
  my (@list) = @_;
  my $vec;
  foreach my $item (@list) {
    $vec .= $item.",";
  }
  return $vec;
};

sub vec2list {
  my ($vec) = @_;
  return split(',',$vec);
};

sub printVec {
  my (@list) = @_;
  my $line;
  if($#list == 0) { #list
    my $vec = $list[0];
    @list = vec2list($vec);
  }
  $line = "(";
  foreach my $num (@list) {
    $line .= fixnum33($num).", ";
  }
  $line .= ")";
  return $line;
};

sub dotProd {
  my ($v1,$v2) = @_;
  my $dot;
  my ($x1,$y1,$z1) = vec2list($v1);
  my ($x2,$y2,$z2) = vec2list($v2);
  $dot = $x1 * $x2 + $y1 * $y2 + $z1 * $z2;
  return $dot;
};

sub distVec {
  my ($v1,$v2) = @_;
  my ($x1,$y1,$z1) = vec2list($v1);
  my ($x2,$y2,$z2) = vec2list($v2);
  return sqrt(($x1-$x2)**2+($y1-$y2)**2+($z1-$z2)**2);
};

sub distVec2D {
  my ($v1,$v2) = @_;
  my ($x1,$y1) = vec2list($v1);
  my ($x2,$y2) = vec2list($v2);
  return sqrt(($x1-$x2)**2+($y1-$y2)**2);
};

sub crossVec {
  my ($v1,$v2) = @_;
  my ($cx,$cy,$cz);
  my ($x1,$y1,$z1) = vec2list($v1);
  my ($x2,$y2,$z2) = vec2list($v2);
  $cx = $y1*$z2 - $z1*$y2;
  $cy = $x1*$z2 - $z1*$x1;
  $cz = $x1*$y2 - $y1*$x2;
  return list2vec($cx,$cy,$cz);
};

sub crossProd {
  my ($v1,$v2) = @_;
  my $dot;
  my ($x1,$y1,$z1) = vec2list($v1);
  my ($x2,$y2,$z2) = vec2list($v2);
  $dot = $x1 * $x2 + $y1 * $y2 + $z1 * $z2;
  return sqrt(1 - $dot**2);
};

sub getComp {
  my ($v,$num);
  my @c = vec2list($v);
  return $c[$num];
};

return 1;
